
# coding=utf-8
import aiomysql
import asyncio

# C7：输入: 材料ID => 输出:该pdf所有问题ID列表
async def material_full_text(material_id: int):
    '''
    :param material_id: 材料ID
    :return full_text_problem_id_list: 该pdf所有问题ID列表
    '''

    conn = None
    cursor = None
    try:

        conn = await aiomysql.connect(
            host='192.168.2.244',
            port=3306,
            user='root',
            password='linkAge@12345',
            charset='utf8mb4',
            db='stock_exchange'
        )

        cursor = await conn.cursor()
        await cursor.execute("SELECT problem_id FROM material WHERE material_id = %s", (material_id,))
        material_results = await cursor.fetchone()
        await cursor.execute("SELECT project_id, round, file_name, datetime FROM problem WHERE problem_id = %s", (material_results[0],))
        problem_results = await cursor.fetchone()

        await cursor.execute("SELECT problem_id, problem_order FROM problem WHERE project_id = %s AND round = %s AND file_name = %s AND datetime = %s",
                             (problem_results[0], problem_results[1], problem_results[2], problem_results[3]))
        Problem_results = await cursor.fetchall()
        full_text_problem_id_list = []
        for Problem in Problem_results:
            full_text_problem_id_list.append(Problem[0])
        # print(full_text_problem_id_list)

        return full_text_problem_id_list

    except Exception as e:
        # print(f"C7 error: {e}")
        return f"C7 error : {e}"
    finally:
        if cursor:
            await cursor.close()  # 使用 await 关键字
        if conn:
            conn.close()

if __name__ == '__main__':
    import asyncio

    material_id = 22
    full_text_problem_id_list = asyncio.run(material_full_text(material_id))
    print(full_text_problem_id_list)

